using System;
using System.Collections;
using System.Xml;
using System.Net;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	/// <summary>
	/// Summary description for FormDataObjectLoadWorker.
	/// </summary>
	class FormDataQueryWorker
	{
        //CID 1142 //2012.03.20 //throwsException
		public static void query( Form form, bool throwsException )
        {
            //CID 1138 //2012.03.19 //Begin
            form.OperationFailed = false;
            //CID 1138 //2012.03.19 //End
			Query query = form.Manifest.Query;
			//CID 0474 //2009.12.23 //Begin
			form.Logger.LogUpdate(null);
			//CID 0474 //2009.12.23 //End
			string queryName = "";
			try{
				if( query.WSAdapter != null ){
					queryName = query.WSAdapter.ServiceUrl;
					DataWSAdapter wsAdapter = (DataWSAdapter)DataAdapterTableUtil.getAvaliableAdapter( form, query.WSAdapter );
					DataObjectWSQueryWorker.queryDoc( form, wsAdapter );
				}
				if( query.AdoAdapter != null ){
					queryName = query.AdoAdapter.TableName;
					DataADOAdapter adoAdapter = (DataADOAdapter)DataAdapterTableUtil.getAvaliableAdapter( form, query.AdoAdapter );
					DataObjectDBQueryWorker.queryDoc( form, adoAdapter);
				}
                //CID 1104 //2012.02.21 //Begin
                if (query.SPListAdapter != null)
                {
                    queryName = query.SPListAdapter.Name;
                    DataSPListAdapter spAdapter = (DataSPListAdapter)DataAdapterTableUtil.getAvaliableAdapter(form, query.SPListAdapter);
                    DataObjectSPQueryWorker.queryDoc(form, spAdapter);
                }
                //CID 1104 //2012.02.21 //End
			}catch( Exception e ){
				bool triedAgain = false;
				try
				{
					if( query.WSAdapter != null )
					{
						UnauthorizedConnection checkedConn = DataWebUnauthorizedWorker.checkUnauthorizedWebAdapter( form, query.WSAdapter, e );
						if( checkedConn != null )
						{
							UnauthorizedConnection loginedConn = DataWebUnauthorizedWorker.findLoginConnection( form, query.WSAdapter );
							if( loginedConn != null )
							{
								triedAgain = true;
								DataWSAdapter wsAdapter = (DataWSAdapter)DataAdapterTableUtil.getAvaliableAdapter( form, query.WSAdapter );
								DataObjectWSQueryWorker.queryDoc( form, wsAdapter, loginedConn );
								form.UnauthorizedConnectionList.Remove( checkedConn );
							}
						}
					}
				}
				catch( Exception exec )
				{
                    //CID 1138 //2012.03.19 //Begin
                    form.OperationFailed = true;
                    //CID 1138 //2012.03.19 //End
                    //CID 1142 //2012.03.20 //Begin
                    if (throwsException)
                    {
                        throw new InfoJetException(MessageUtil.Error2Query() + queryName, null);
                    }
                    else
                    {
                        FormUtil.addModalMessage(form, MessageUtil.Error2Query() + queryName, exec, query.WSAdapter != null);
                    }
                    //CID 1142 //2012.03.20 //End
				}

				if( !triedAgain )
                {
                    //CID 1138 //2012.03.19 //Begin
                    form.OperationFailed = true;
                    //CID 1138 //2012.03.19 //End
                    //CID 1142 //2012.03.20 //Begin
                    if (throwsException)
                    {
                        throw new InfoJetException(MessageUtil.Error2Query() + queryName, null);
                    }
                    else
                    {
                        FormUtil.addModalMessage(form, MessageUtil.Error2Query() + queryName, e, query.WSAdapter != null);
                    }
                    //CID 1142 //2012.03.20 //End
				}
			}
		}
	}
}
